package com.nuoniu.sibanyun.service.crm.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.crm.CrmStrategy;
import com.nuoniu.sibanyun.mapper.crm.CrmStrategyMapper;
import com.nuoniu.sibanyun.service.crm.ICrmStrategyService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 客户策略（新） 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-15
 */
@Service
public class CrmStrategyServiceImpl extends ServiceImpl<CrmStrategyMapper, CrmStrategy> implements ICrmStrategyService {

    @Autowired
    CrmStrategyMapper strategyMapper;

    @Override
    public List<CrmStrategy> getCrmStrategyList() {
        UserInfo userInfo = UserThreadLocal.get();
        CrmStrategy crmStrategy = new CrmStrategy();
        QueryWrapper<CrmStrategy> queryWrapper = new QueryWrapper<>(crmStrategy);
        queryWrapper.eq("company_id", userInfo.getCompanyId());
        queryWrapper.eq("status", 0);
        List<CrmStrategy> crmStrategyList = list(queryWrapper);
        if(crmStrategyList.size() == 0) {
            strategyMapper.saveAll(userInfo.getCompanyId().longValue(), userInfo.getCompanyName(), userInfo.getUserId().longValue(), userInfo.getRealName());
            crmStrategyList = list(queryWrapper);
        }
        return crmStrategyList;
    }

    @Override
    public void saveCrmStrategy(Map<String, String> param) {
        UserInfo userInfo = UserThreadLocal.get();
        for(String key : param.keySet()){
            String value = param.get(key);
            if(key.equals("crm_2_auxiliary")) {
                strategyMapper.updateAuxiliary(userInfo.getCompanyId(), "crm_2", value);
                continue;
            }

            if(key.equals("crm_2_userNames")) {
                strategyMapper.updateUserNems(userInfo.getCompanyId(), "crm_2", value);
                continue;
            }

            if(key.equals("crm_7_auxiliary")) {
                strategyMapper.updateAuxiliary(userInfo.getCompanyId(), "crm_7", value);
                continue;
            }

            if(key.equals("crm_7_userNames")) {
                strategyMapper.updateUserNems(userInfo.getCompanyId(), "crm_7", value);
                continue;
            }

            strategyMapper.updateVal(userInfo.getCompanyId(), key, value);
        }
    }

    @Override
    public CrmStrategy getStrategyByCode(String code, Integer companyId) {
        return strategyMapper.getStrategyByCode(code, companyId);
    }
}
